set shell=powershell " Default Windows shell is cmd.exe
set mouse=a " mouse on

" Default directory
cd ~
set autochdir

let s:prefix="<C-a>" " <prefix>
let s:vmuxExit='<C-\><C-n>'

" Use system clipboard
set clipboard=unnamed


" --- FUNCTIONS ---
function Bind(key, ...)
	exe join(['tnoremap <silent>', s:prefix . a:key, s:vmuxExit, join(a:000, ' ')], ' ')
	exe join(['nnoremap <silent>', s:prefix . a:key, s:vmuxExit, join(a:000, ' ')], ' ')
endfunction
command! -nargs=+ -bar Bind :call Bind(<f-args>)

function GoToTab(index)
	if a:index == tabpagenr()
		startinsert
	elseif a:index <= tabpagenr("$")
		call feedkeys(a:index . "gt")
	else
		echo "Can't find window: " . a:index
		startinsert
	endif
endfunction

function ModeName()
	let modes = {
		\ '__' : '------',
		\ 'n'  : 'NORMAL',
		\ 'no' : 'OP PENDING',
		\ 'nov': 'OP PENDING CHAR',
		\ 'noV': 'OP PENDING LINE',
		\ 'no': 'OP PENDING BLOCK',
		\ 'niI': 'INSERT (NORMAL)',
		\ 'niR': 'REPLACE (NORMAL)',
		\ 'niV': 'V REPLACE (NORMAL)',
		\ 'v'  : 'VISUAL',
		\ 'V'  : 'VISUAL-LINE',
		\ '' : 'VISUAL-BLOCK',
		\ 's'  : 'SELECT',
		\ 'S'  : 'SELECT-LINE',
		\ '' : 'SELECT-BLOCK',
		\ 'i'  : '',
		\ 'ic' : 'INSERT COMPL GENERIC',
		\ 'ix' : 'INSERT COMPL',
		\ 'R'  : 'REPLACE',
		\ 'Rc' : 'REPLACE COMP GENERIC',
		\ 'Rv' : 'V REPLACE',
		\ 'Rx' : 'REPLACE COMP',
		\ 'c'  : 'COMMAND',
		\ 'cv' : 'VIM EX',
		\ 'ce' : 'EX',
		\ 'r'  : 'PROMPT',
		\ 'rm' : 'MORE PROMPT',
		\ 'r?' : 'CONFIRM',
		\ '!'  : 'SHELL',
		\ 't'  : '',
		\ 'multi' : 'MULTI',
	\ }
	return modes[mode()]
endfunction

" --- BINDINGS ---
Bind % :vnew +term<Enter> " split window vertical
Bind \" :new +term<Enter> " split window horizontal

Bind x :q!<Enter> " close pane
Bind X :qa!<Enter> " exit

Bind [ " swich to normal mode

Bind c :tabnew +term<Enter> " create tab
Bind n gt " next tab
Bind p gT " previos tab

Bind : :call feedkeys(':')<Enter> " command mode

" Window moving <prefix>h
Bind j <C-w>j
Bind h <C-w>h
Bind H <C-w>H
Bind J <C-w>J
Bind k <C-w>k
Bind K <C-w>K
Bind l <C-w>l
Bind L <C-w>L

" Tab moving <prefix>1
Bind 1 :call GoToTab(1)<Enter>
Bind 2 :call GoToTab(2)<Enter>
Bind 3 :call GoToTab(3)<Enter>
Bind 4 :call GoToTab(4)<Enter>
Bind 5 :call GoToTab(5)<Enter>
Bind 6 :call GoToTab(6)<Enter>
Bind 7 :call GoToTab(7)<Enter>
Bind 8 :call GoToTab(8)<Enter>
Bind 9 :call GoToTab(9)<Enter>
Bind 0 :call GoToTab(10)<Enter>


" --- AUTOCOMMANDS ---
" Launch terminal on startup
au VimEnter * term

" Enter terminal mode automatically
au BufWinEnter,WinEnter term://* startinsert

" Close the window if the terminal is finished
au TermClose * :q! | startinsert


" --- UI ---
" Transparent window borders
hi VertSplit cterm=NONE

" Hide UI
set noshowmode
set noruler
set showtabline=0
set noshowcmd

" Statusline
set laststatus=3 " single statusline
set statusline= " reset statusline
set statusline+=[%{tabpagenr()}/%{tabpagenr('$')}] " tab-number and count of tabs
set statusline+=\ %{ModeName()} " current mode (not INSERT and TERMINAL)
set statusline+=%= " float: right
set statusline+=%{strftime('%R\ %d-%m-%Y')}\  " date and time
" Transparent background for statusline
hi StatusLine ctermbg=0 cterm=NONE


" Start insert on startup
startinsert
